#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Project:     rest_flask
# @File:        vue_api.py
# @Author:      Fan GuiChuan
# @Date:        2025/8/15 10:59
# @Version:     Python3.7
# @Description:
import os

from flask import send_from_directory


class VueMgrApi:
    """
    文件管理接口类
    """

    def __init__(self, logger, blueprint):
        self.logger = logger
        self.blueprint = blueprint
        self.blueprint.static_folder = os.path.join(os.environ['root_path'], 'static/')
        self.register_blueprint()

    def register_blueprint(self):
        """注册文件管理相关的API路由"""
        self.blueprint.add_url_rule('.favicon.ico', view_func=self.favicon)
        self.blueprint.add_url_rule('/platform-config.json', view_func=self.platform_config)
        self.blueprint.add_url_rule('/', view_func=self.index)
        self.blueprint.add_url_rule('/static/js/<filename>', view_func=self.serve_js)
        self.blueprint.add_url_rule('/static/css/<filename>', view_func=self.serve_vue_static_dist)
        self.blueprint.add_url_rule('/assets/<filename>', view_func=self.serve_assets)

    def platform_config(self):
        return send_from_directory(os.path.join(self.blueprint.static_folder, 'dist'), 'platform-config.json')

    def serve_js(self, filename):
        self.logger.debug(f"serv js: {filename}")
        response = send_from_directory(os.path.join(self.blueprint.static_folder, 'dist/static/js'), filename)
        response.headers['Content-Type'] = 'application/javascript'
        return response

    def serve_assets(self, filename):
        self.logger.debug(f"serv js: {filename}")
        response = send_from_directory(os.path.join(self.blueprint.static_folder, 'dist/assets'), filename)
        # response.headers['Content-Type'] = 'application/javascript'
        return response

    def favicon(self):
        return send_from_directory(os.path.join(self.blueprint.static_folder, 'dist'), 'favicon.ico')

    def index(self):
        return send_from_directory(os.path.join(self.blueprint.static_folder, 'dist'), 'index.html')

    def serve_vue_static_dist(self, filename):
        self.logger.debug(f"filename: {filename}")
        return send_from_directory(os.path.join(self.blueprint.static_folder, 'dist'), filename)
